package com.zuoye.experiment2.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import com.zuoye.experiment2.service.ISkillsService;
import com.zuoye.experiment2.entity.Skills;
import org.springframework.web.servlet.view.RedirectView;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author coolwen
 * @since 2024-05-08 16:35:27
 */
@Controller
@RequestMapping("/skills")
@Slf4j
public class SkillsController {

    @Autowired
    ISkillsService skillService;

    @GetMapping("/list")
    public ResponseEntity<List<Skills>> SkillsList() {
        List<Skills> skills = skillService.list();
        return ResponseEntity.ok(skills);
    }

    @GetMapping("/showskills")
    public String listSkills(Model model) {
        List<Skills> skills = skillService.list();
        model.addAttribute("showSkills", skills);
        return "skills/showSkills";
    }

    @GetMapping("/addskills")
    public String addSkills(Model model) {
        return "skills/addSkills";
    }

    @PostMapping("/addskills")
    public String saveSkills(@ModelAttribute("skills") Skills skills) {
        skillService.save(skills);
        return "redirect:/heroes/showheroes";
    }

    @GetMapping("/update/{id}")
    public String editSkillsForm(@PathVariable Integer id, Model model) {
        Skills skills = skillService.getById(id);
        model.addAttribute("updateSkills", skills);
        return "skills/updateSkills";
    }

    @PostMapping("/update/{id}")
    public RedirectView updateSkills(@ModelAttribute("skills") Skills skills) {
        skillService.updateById(skills);
        return new RedirectView("/heroes/showheroes");
    }

    @PostMapping("/delete/{id}")
    public String deleteSkills(@PathVariable Integer id) {
        skillService.removeById(id); // 假设这个方法不会抛出运行时异常
        return "redirect:/heroes/showheroes"; // 重定向到显示英雄的页面
    }

}
